
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:eshi/app/shi/data/HistoryPoint.dart';

/// 时间线绘制 
class TimelinePainter extends CustomPainter {
  final DateTime startTime;
  final DateTime endTime;
  final List<HistoryPoint> events;
  int get limit{
        return min(endTime.year-startTime.year,45);
  }
   
  TimelinePainter({
    required this.startTime,
    required this.endTime,
    required this.events,
  });
  
  @override
  void paint(Canvas canvas, Size size) {
    final double totalSeconds = endTime.difference(startTime).inSeconds.toDouble();
    final bool isHorizontal = size.width>size.height;
    final double pixelsPerSecond = isHorizontal? size.width / totalSeconds : size.height / totalSeconds;
    
    // canvas.drawColor(Colors.black, BlendMode.srcOver);

    // 绘制时间轴线
    final linePaint = Paint()
        ..color = Colors.grey
        ..style = PaintingStyle.fill
        ..strokeWidth = 2.0;
      
    canvas.drawLine(
      isHorizontal ? Offset(0, size.height / 2): Offset(size.width/2, 0),
      isHorizontal ? Offset(size.width, size.height / 2) : Offset(size.width/2, size.height),
      linePaint,
    );

    // 绘制主要刻度
    final hourPaint = Paint()
      ..color = Colors.grey
      ..strokeWidth = 1.0;
      
    
    // 绘制刻度
    var currentHour = DateTime(startTime.year, startTime.month, startTime.day, startTime.hour);
    int ds = (endTime.difference(startTime).inSeconds/limit).toInt();
    var limitDuration = Duration(seconds: ds);
    while (currentHour.isBefore(endTime)) {
      final double x = currentHour.difference(startTime).inSeconds * pixelsPerSecond;
      canvas.drawLine(
        isHorizontal ? Offset(x, size.height / 2) : Offset(size.width/2, x),
        isHorizontal ? Offset(x, size.height / 2 + 10) : Offset(size.width/2-10, x),
        hourPaint,
      );
      
      // 绘制时间文本
      final textPainter = TextPainter(
        text: TextSpan(
          text: '${currentHour.year}',
          style: TextStyle(color: Colors.black, fontSize: 10),

        ),
        textDirection: TextDirection.ltr,
      );
      textPainter.layout();
      textPainter.paint(canvas, 
        isHorizontal? Offset(x - 15, size.height / 2 + 15) : Offset(size.width/2 - 35, x-10)
      );
      
      currentHour = currentHour.add(limitDuration);
    }

    // 绘制事件标记
    final eventPaint = Paint()
      ..color = Colors.red
      ..style = PaintingStyle.fill;
    
    double beforX =0;
    for (final eventTime in events) {
        final double x = eventTime.getStartTime().difference(startTime).inSeconds * pixelsPerSecond;
        canvas.drawCircle(
          isHorizontal ? Offset(x, size.height / 2): Offset(size.width/2, x),
          4.0,
          eventPaint,
        );
        
        //事件
        double X=0;
        if(isHorizontal) {
          X = beforX>0 && x-beforX<20? x:x-10;
        } else {
          X = beforX>0 && x-beforX>10? x:x+10;
        } 
        
        var eventName = '${eventTime.startTime} ${eventTime.name}';
        if(eventTime.startTime == eventTime.endTime){
          eventName = eventTime.name;
        }else if(eventTime.startTime.toString() == eventTime.name){
          eventName = '${eventTime.startTime}';
        }
        final textPainter = TextPainter(
          text: TextSpan(
            text: eventName,
            style: TextStyle(color: Colors.black, fontSize: 8),
            
          ),
          textDirection: TextDirection.ltr,
        );
        textPainter.layout();
        textPainter.paint(canvas, 
          isHorizontal ? Offset(X, X!=x? size.height / 2 - 30: size.height / 2 - 15) : Offset(size.width/2+5,X-5)
        );

        beforX = x;
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}